<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Activity extends Model
{
    protected $table = 'activity';//活动表
    public $timestamps = false;
    protected $guarded = [
       ];

    //活动列表
    public function getData($search = [], $page = 1, $length = 20){
        $activityList = array();
        $activityList['pageData'] = array();
        $date = date('Y-m-d H:i:s', time());
        $ClassifyModel = new Classify();
        $UserModel = new AdminUser();

       $sql = \DB::table($this->table)
            ->leftjoin($ClassifyModel->getTable() . ' as c','c.id','=','activity.class_id')
            ->leftjoin($UserModel->getTable() . ' as u','u.id','=','activity.user_id')
            ->select('c.classname','u.name','activity.strength', 'activity.surplus_number', 'activity.view_index','activity.sort','activity.set_out','activity.duration','activity.destination','activity.id','activity.title','activity.img','activity.route','activity.route_info','activity.cost','activity.cost_info','activity.limit_of_number','activity.on_line_time','activity.off_line_time')
            ->whereNull('activity.deleted_at')
            ->where('activity.on_line_time', '<=', $date);

       if(isset($search['type']) && $search['type'] == 'lishi') {
           $sql->where('activity.off_line_time', '<=', $date);
       }else{
           $sql->where('activity.off_line_time', '>', $date);
       }


        $activityList['pageData']['count']     = $sql->count();
        $activityList['pageData']['lastPage']  = ceil($activityList['pageData']['count'] / $length);

        if(isset($search['title'])){
            $sql->where('activity.title', 'like', "%".$search['title']."%");
        }
        if(isset($search['cid'])){
            $sql->where('activity.class_id',  $search['cid']);
        }
        if(isset($search['id'])){
            $sql->where('activity.id',  $search['id']);
        }
        if(isset($search['user'])){
            $sql->where('u.name', 'like', "%".$search['user']."%");
        }

        $activityList['list'] = $sql
            ->orderBy('activity.sort', 'asc')
            ->skip(($page-1) * $length)->take($length)
            ->get();

        return $activityList;
    }
    //手机版活动列表
    public function  morindex(){
        $date = date('Y-m-d H:i:s', time());
        $activityList = array();
        $sql = \DB::table($this->table)
            ->select('title','id')
            ->whereNull('deleted_at')
            ->where('activity.on_line_time', '<=', $date)
            ->where('activity.off_line_time', '>', $date);
        $activityList['count']  = $sql->count();
        $activityList['list']=$sql->get();
        return $activityList;
    }
    //手机版活动报名信息
    public function morEnroll($id){
        $enrollList = array();

        $enrollModel = new Enroll();
        $personalModel = new Personal();
        $enrollPersonalModel = new EnrollPersonal();
        $sql = \DB::table($enrollModel->getTable() . ' as e')
            ->leftJoin($enrollPersonalModel->getTable() . ' as ep', 'ep.enroll_id', '=', 'e.id')
            ->leftJoin($personalModel->getTable() . ' as p', 'p.id', '=', 'ep.personal_id')
            ->select('e.id as id','e.activity_id','p.real_name','p.mobile','p.idcard','p.id as pid', 'ep.personal_id', 'ep.enroll_id','ep.reason')
            ->where('e.activity_id',$id)
            ->whereNull('p.deleted_at')
            ->whereNull('e.deleted_at')
            ->whereNull('ep.deleted_at');
        $enrollList['count'] = $sql->count('ep.personal_id');
        $enrollList['list'] = $sql->get();
        return $enrollList;
    }
    //手机版历史报名信息
    public function morHisEnroll($id){
        $enrollList = array();
        $enrollModel = new Enroll();
        $personalModel = new Personal();
        $enrollPersonalModel = new EnrollPersonal();

        $sql = \DB::table($enrollModel->getTable() . ' as e')
            ->leftJoin($enrollPersonalModel->getTable() . ' as ep', 'ep.enroll_id', '=', 'e.id')
            ->leftJoin($personalModel->getTable() . ' as p', 'p.id', '=', 'ep.personal_id')
            ->select('e.id as id','e.activity_id','p.real_name','p.mobile','p.idcard','p.id as pid', 'ep.personal_id', 'ep.enroll_id')
            ->where('e.activity_id',$id)
            ->whereNull('p.deleted_at')
            ->whereNull('e.deleted_at')
            ->where('ep.deleted_at','<>','');
        $enrollList['count'] = $sql->count('ep.personal_id');
        $enrollList['list'] = $sql->get();
        return $enrollList;
    }
    //获取领队活动信息
    public function Login($search = [], $page = 1, $length = 20, $name){
        $activityList = array();
        $activityList['pageData'] = array();
        $UserModel = new AdminUser();
        $ClassifyModel = new Classify();
        $date = date('Y-m-d H:i:s', time());

        $sql = \DB::table($this->table  . ' as a')
            ->leftjoin($ClassifyModel->getTable() . ' as c','c.id','=','a.class_id')
            ->leftjoin($UserModel->getTable() . ' as u','u.id','=','a.user_id')
            ->select('u.id','u.name','a.sort','a.id as aid','a.user_id','c.classname','a.set_out','a.duration','a.destination','a.id','a.title','a.img','a.route','a.cost','a.limit_of_number')
            ->whereNull('a.deleted_at')
            ->where('u.name',$name)
            ->where('a.on_line_time', '<=', $date)
            ->where('a.off_line_time', '>', $date);

        $activityList['pageData']['count']     = $sql->count();
        $activityList['pageData']['lastPage']  = ceil($activityList['pageData']['count'] / $length);

        if(isset($search['title'])){
            $sql->where('a.title', 'like', "%".$search['title']."%");
        }
        if(isset($search['user'])){
            $sql->where('u.name', 'like', "%".$search['user']."%");
        }

        $activityList['list'] = $sql
            ->orderBy('a.sort', 'asc')
            ->skip(($page-1) * $length)->take($length)
            ->get();

        return $activityList;
    }

    //活动添加
    public function saveData($data){
        return \DB::table($this->table)->insert($data);
    }
    public function Class(){
        $ClassifyModel = new Classify();
        return \DB::table($ClassifyModel->getTable())
            ->select('classname','id')
            ->whereNull('deleted_at')
            ->get();
    }
    public function user(){
        $userModel = new AdminUser();
        return \DB::table($userModel->getTable())->select('name','id')
            ->whereNull('deleted_at')
            ->get();
    }
    //活动复制
    public function Copy($data){
        return \DB::table($this->table)->insert($data);
    }

    //活动修改
    public function saveEdit($data,$id){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }
    //活动删除
    public function Del($data,$id){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }
    //图片首页
    public function get($search = [], $page = 1, $length = 20 ,$id){
        $imgsList = array();
        $imgsList['pageData'] = array();
        $AcitivityImgsModel = new ActivityImgs();
        $sql = \DB::table($AcitivityImgsModel->getTable() .' as i')
            ->leftjoin($this->table .' as a','i.activity_id','=','a.id')
            ->select('a.title as tit','a.id as aid','i.title','i.deleted_at','i.img','i.id as iid','i.updated_at')
            ->where('i.activity_id',$id)
            ->whereNull('i.deleted_at');

        $imgsList['pageData']['count']     = $sql->count();
        $imgsList['pageData']['lastPage']  = ceil($imgsList['pageData']['count'] / $length);

        $imgsList['list'] = $sql->orderBy('i.sort', 'desc')
            ->skip(($page-1) * $length)->take($length)
            ->get();

        return $imgsList;
    }

    //图片添加
   public function imgsadd($data){
       $AcitivityImgsModel = new ActivityImgs();
       return \DB::table($AcitivityImgsModel->getTable())->insert($data);
    }
    public function image(){
        $imgcateModel= new ImgcateImg();
        return \DB::table($imgcateModel->getTable())->select('id','img')->get();
    }
    //图片删除
    public function imgsdel($data,$id){
        $AcitivityImgsModel = new ActivityImgs();
        return \DB::table($AcitivityImgsModel->getTable())->where('id',$id)->update($data);
    }

    //报名首页
    public function enroll($search = []){
        $enrollList = array();
        $enrollList['pageData'] = array();

        $enrollModel = new Enroll();
        $userModel = new User();

        $sql = \DB::table($enrollModel->getTable() . ' as e')
            ->leftJoin($userModel->getTable() . ' as u', 'u.id', '=', 'e.u_id')
            ->select('e.id as eid','u.id as uid','e.u_id','e.has_child','u.nick_name','u.integral','e.activity_id')
            ->whereNull('e.deleted_at')
            ->whereNull('u.deleted_at');

        if(isset($search['activity_id'])){
            $sql->where('activity_id', $search['activity_id']);
        }

        $enrollList['list'] = $sql->get();

        return $enrollList;
    }

    public function getEnrollPersonal($enrollIds){
        $personalModel = new Personal();
        $enrollPersonalModel = new EnrollPersonal();

        return \DB::table($enrollPersonalModel->getTable() . ' as ep')
            ->leftJoin($personalModel->getTable() . ' as p', 'p.id', '=', 'ep.personal_id')
            ->select(
                'p.real_name',
                'p.id as pid',
                'p.u_id',
                'p.mobile',
                'p.idcard',
                'p.relation',
                'ep.enroll_id',
                'ep.personal_id',
                'ep.reason')
            ->whereIn('ep.enroll_id', $enrollIds)
            ->whereNull('ep.deleted_at')
            ->get();
    }
    public function HistoryEnroll($enrollIds){
        $personalModel = new Personal();
        $enrollPersonalModel = new EnrollPersonal();
        return \DB::table($enrollPersonalModel->getTable() . ' as ep')
            ->leftJoin($personalModel->getTable() . ' as p', 'p.id', '=', 'ep.personal_id')
            ->select(
                'p.real_name',
                'p.id as pid',
                'p.u_id',
                'p.mobile',
                'p.idcard',
                'p.relation',
                'ep.enroll_id',
                'ep.personal_id',
                'ep.deleted_at')
            ->whereIn('ep.enroll_id', $enrollIds)
            ->where('ep.deleted_at','<>','null')
            ->get();
    }

    //积分修改
    public function integralEdit($data,$id){
        $userModel = new User();
        return \DB::table($userModel->getTable())->where('id',$id)->update($data);
    }

    //报名联系人修改、删除
    public function personalupdate($enrollId, $personalId,$data){
        $personalModel = new EnrollPersonal();
        $enrollModel = new Enroll();

        if(isset($data['deleted_at'])) {
            $personalNum = \DB::table($personalModel->getTable())
                ->where('enroll_id', $enrollId)
                ->whereNull('deleted_at')
                ->count();

            if ($personalNum === 1) {
                \DB::table($enrollModel->getTable())->where('id', $enrollId)->update([
                    'deleted_at' => date('Y-m-d H:i:s', time())
                ]);
            }
        }

        return \DB::table($personalModel->getTable())
            ->where('personal_id',$personalId)
            ->where('enroll_id',$enrollId)
            ->update($data);
    }


}